
/*
 * @(#)WorkflowDueDateTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.taglib;

import java.sql.Timestamp;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.WorkflowProgress;

/**
 * @author           Tony Chen
 * @company          DCIVision Limited
 * @creation date    12/10/2005
 * @version          $Revision: 1.3.2.2 $
 */
public class WorkflowDueDateTag extends BodyTagSupport {
  public static final String REVISION = "$Revision: 1.3.2.2 $";

  protected Log log = LogFactory.getLog(this.getClass().getName());

  private String status = null;
  private Timestamp dueDate = null;
  
  
	public Timestamp getDueDate() {
		return dueDate;
	}
  
	public void setDueDate(Timestamp dueDate) {
		this.dueDate = dueDate;
	}
  
  public String getStatus() {
	  return status;
  }

  public void setStatus(String status) {
	  this.status = status;
  }
  
  public WorkflowDueDateTag(){
    super();
  }
    
  public int doStartTag() throws JspException {
    try {
      init();
      pageContext.getOut().println(this.getContent()) ;//Edit By TC for Ajax-Used
    } catch (ApplicationException ae){
    	log.error(ae.getMsgCode(), ae);
    } catch (Exception e) {
    	log.fatal(e.getMessage(), e);
    } finally {
      this.release();
    }
    return(SKIP_BODY);
  }

  public int doEndTag() throws JspException {
    return EVAL_PAGE;
 }

  public void release() {
    super.release();
    this.setDueDate(null);
    this.setStatus(null);
  }

  protected boolean validateTag(){
    return ( status!=null);
  }

  protected String getContent() throws Exception {
    //JspWriter out = pageContext.getOut();    
    StringBuffer content = new StringBuffer();
    String dueDateLable = "";
    String color = "";
    
    SessionContainer sessionContainer = (SessionContainer)this.pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    
    long seconds = 0;
    if (Utility.isEmpty(this.getDueDate())) {
      content.append("-");
    } else if (WorkflowProgress.STATUS_PENDING.equals(status) || WorkflowProgress.STATUS_IN_PROGRESS.equals(status) ) {
      seconds = (Utility.getCurrentTimestamp().getTime() - this.getDueDate().getTime()) / 1000;
      if (seconds > 0) {
        dueDateLable = "workflow.label.delay_sentence";
        color = "red";
      } else {
        dueDateLable = "workflow.label.due_date_remain";
        seconds = - seconds;
        color = "black";
      }
      
      int days = new Float( ((float)seconds) / 86400.0F ).intValue();
      int hours = new Float((seconds - (days * 86400.0F)) / (3600.0F)).intValue();
      int minutes = new Float((seconds - (days * 86400.0F) - (hours * 3600.0F)) / (60.0F)).intValue();
      
      content.append("<a style=\"text-decoration: none; cursor:text; \"  onmouseover=\"return getPopupInfo(\'" +
      			(MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), dueDateLable, String.valueOf(days), String.valueOf(hours), String.valueOf(minutes) ) )+
      	    "\');\" onmouseout=\"return nd();\">");
      content.append("<font color=\'" + color + "\'>" + (TextUtility.getDateTimeFormat().format(this.getDueDate()))+ "</font> </a>");
    
    } else {
      content.append("<font color=\'black\'>" + (TextUtility.getDateTimeFormat().format(this.getDueDate())) + "</font> " );
    }
    return content.toString();
    //out.println(content.toString());
  }

  protected void init() throws ApplicationException {    
    if ( !validateTag() ){
      throw new IllegalArgumentException();
    }
  }
 
}


